﻿#include "Stat.h"

std::map<Stat, int> Stats::getValues() const
{
    return Values;
}

void Stats::setValues(const std::map<Stat, int> &value)
{
    Values = value;
}

int Stats::getCount() const
{
    return getValues().Sum([&] (std::any pair)
    {
        std::abs(pair->Value);
    });
}

int &Stats::operator [](Stat stat)
{
    int result;
    std::unordered_map<Stat, int>::const_iterator Values_iterator = Values.find(stat);
    auto tempVar = Values_iterator == getValues().end() ? 0 : result;
    result = Values_iterator->second;
    return tempVar;
}

//C# TO C++ CONVERTER TODO TASK: You cannot specify separate 'set' logic for indexers in C++:
//void Stats::setIndexer(Stat stat, int value)
//{
//    if (value == 0)
//    {
//        if (getValues().find(stat) != getValues().end())
//        {
//            getValues().erase(stat);
//        }
//
//        return;
//    }
//
//    getValues()[stat] = value;
//}

Stats::Stats()
{
}

Stats::Stats(const Stats &stats)
{
    for (auto pair : stats)
    {
        this->operator[](pair.second::Key) += pair.second->Value;
    }
}

Stats::Stats(BinaryReader *reader)
{
    int count = reader->ReadInt32();

    for (int i = 0; i < count; i++)
    {
        getValues()[static_cast<Stat>(reader->ReadByte())] = reader->ReadInt32();
    }
}

void Stats::Add(Stats *stats)
{
    for (auto pair : *stats)
    {
        this->operator[](pair.second::Key) += pair.second->Value;
    }
}

void Stats::Save(BinaryWriter *writer)
{
    writer->Write(getValues().size());

    for (auto pair : getValues())
    {
        writer->Write(static_cast<unsigned char>(pair.first));
        writer->Write(pair.second);
    }
}

void Stats::Clear()
{
    getValues().clear();
}

bool Stats::Equals(Stats *other)
{
    if (getValues().size() != other->getValues().size())
    {
        return false;
    }

    for (auto value : getValues())
    {
        if (other[value.first] != value.second)
        {
            return false;
        }
    }

    return true;
}
